import * as express from 'express'
import * as bodyParser from 'body-parser'
import * as helmet from 'helmet'
import * as compression from 'compression'
import * as mongoose from 'mongoose'
import * as cors from 'cors'
import api from './api'

const isProd = process.env.NODE_ENV === 'production'

mongoose.connect(
  isProd ? process.env.MONGO_URL_PROD : process.env.MONGO_URL_DEV,
  {
    useNewUrlParser: true,
    useCreateIndex: true,
    useFindAndModify: false,
  }
)

const app = express()

app.use(cors())
app.use(helmet())
app.use(compression())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

api(app)

app.listen(4000, err => {
  if (err) throw err
})
